package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.service.OrdersService;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 订单管理
 */
@RestController
@RequestMapping("order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 下单
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        ordersService.submit(orders);

        return R.success("下单成功");
    }


    /**
     * 移动端分页查询订单
     */
    @GetMapping("userPage")
    public R<Page<OrdersDto>> selectPage(Integer page, Integer pageSize) {
        Page<OrdersDto> pageInfo = ordersService.selectPage(page, pageSize);
        return R.success(pageInfo);
    }
    /**
     * 客户端分页查询订单
     */
    @GetMapping("/page")
    public R<Page<OrdersDto>> getPage(Integer page, Integer pageSize, String number,
                                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Page<OrdersDto> pageInfo = ordersService.selectByPage(page, pageSize, number,
                beginTime, endTime);
        return R.success(pageInfo);
    }

    /**
     * 修改订单状态
     */
    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders){

        ordersService.lambdaUpdate()
                .eq(Orders::getId,orders.getId())
                .set(Orders::getStatus,orders.getStatus())
                .update();
        return R.success("状态已更新");
    }

    /**
     * 再来一单
     * @param orders
     * @return
     */
    @PostMapping("again")
    public R add(@RequestBody Orders orders){

         ordersService.add(orders);
        return R.success("下单成功");
    }

}
